var over = 0;
var action = 0;
var _order_by = null;

var thisPage = {
    first: 0,
    // 初始化页面
    OnPageLoad: function () {
        $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page", 1);
        thisPage.FetchForPage(1);
    },
    // 得到分页数据
    GetFilter: function (pageIndex) {
        if (pageIndex !== 0) {
            $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page", pageIndex);
        }
        var item = {};
        item['Limit'] = parseInt($("#page-size").val());
        item['PageIndex'] = parseInt($("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page"));
        item['OrderBy'] = _order_by;
        // 设置条件  
        item['UserName'] = $.trim($('#EmployeeName').val()) === '' ? null : $.trim($('#EmployeeName').val());
        item['UserNo'] = $.trim($('#EmployeeNo').val()) === '' ? null : $.trim($('#EmployeeNo').val());

        return JSON.stringify(item);
    },
    //《按钮》显示全部
    FetchAll: function () {
        $("#EmployeeName").val("");
        $("#EmployeeNo").val("");
        $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page", 1);
        thisPage.FetchForPage(1);
    },
    // 得到数据列表信息
    FetchForPage: function (pageIndex) {
        $.ajax({
            type: "POST",
            url: parent.basePath + '/admin/membership/fetchForPage.html',
            timeout: 30 * 1000,
            data: thisPage.GetFilter(pageIndex),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
                // 显示刷新动画
                $("#loading__indicator i").addClass("fa-spin");
                $("#loading__indicator").show();
            }
        }).done(function (data) {
            if (data.Success === true) {
                thisPage.BuildDataTable(data);
            }
        }).fail(function (data) {
            $("#loading").hide();
            ajaxError(data, 1);
        }).always(function () {
            thisPage.first = 1;
            // 隐藏刷新动画
            $("#loading__indicator i").removeClass("fa-spin");
            $("#loading__indicator").hide();
        });
    },
    // 构建数据列表
    BuildDataTable: function (result) {
        // 设置翻页控件
        if (result.TotalPages > 0) {  //总页数
            $("#wrapper__navbar").data("swPagination").options.max_page = result.TotalPages;
        } else {
            $("#wrapper__navbar").data("swPagination").options.max_page = 1;
        }
        if (result.PageIndex > 0) { //当前页数
            $("#wrapper__navbar").data("swPagination").options.current_page = result.PageIndex;
            $("#wrapper__navbar").data("swPagination").setPage(result.PageIndex);
        } else {
            $("#wrapper__navbar").data("swPagination").options.current_page = 1; //当前页数
            $("#wrapper__navbar").data("swPagination").setPage(1);
        }

        $("#tabData01 tbody").empty();
        // 得到数据列表
        result = result.DataList;
        if (result === null) {
            $('#loading').hide();
            return false;
        }
        var content = '';
        if (result.length > 0) {
            for (var i = 0; i < result.length; i++) {
                var id = result[i].userName;
                content += '<tr _mash_id="' + id + '">';
                if (result[i].roleName === 'Administrator') {
                    content += '<td></td>';
                } else {
                    content += '<td class="column__action"><span title="删除" class="trash" onclick="thisPage.DoDelete(\'' + id + '\');"><i class="fa-all fa-trash-o fa-1"></i></span></td>';
                }
                content += '<td>' + formatNullData(result[i].userName) + '</td>';           // 登录名
                content += '<td>' + formatNullData(result[i].employeeName) + '</td>';
                content += '<td>' + formatNullData(result[i].roleName) + '</td>';
                content += '<td style="border-bottom:none; border-right:none;">' + formatNullData(result[i].departmentName) + '</td>';
                content += '</tr>';
                $("#tabData01 tbody").append(content);
                content = '';
            }
        }
        setOneTableRowColor("tabData01");
        $('#loading').hide();
    },
    // 增加系统成员操作
    DoInsert: function () {
        showModal(1095, 538, '选择用户', parent.basePath + '/master/employee/choice.html?flag=1', null,
                function ($dialog) {
                    var _exist = $.inArray($dialog, parent.arrDialog);
                    if (_exist >= 0) {
                        parent.arrDialog.splice($.inArray($dialog, parent.arrDialog), 1);
                    }
                    // 选择设置
                    if (parent.choice_success) {
                        var _result = new Array();
                        // $("#form_data").data("changed", true);
                        for (var i = 0; i < parent.arrChoice[0].length; i++) {
                            var item = {};
                            item['employeeId'] = parent.arrChoice[0][i].EmployeeId;
                            item['employeeName'] = parent.arrChoice[0][i].EmployeeName;
                            item['userName'] = parent.arrChoice[0][i].EmployeeNo;
                            item['createdBy'] = parseInt($.trim($("#UserId").val()));
                            item['updatedBy'] = parseInt($.trim($("#UserId").val()));

                            _result.push(item);
                        }

                        // 保存，根据所选成员用户，增加明细表记录
                        thisPage.OnInsert(_result);
                    }
                    // 清空选择结果
                    parent.arrChoice.splice(0, parent.arrChoice.length);
                    parent.choice_success = false;
                });
    },
    OnInsert: function (data) {
        $.ajax({
            type: "POST",
            url: parent.basePath + "/admin/membership/insert.html",
            timeout: 30 * 1000,
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            if (parseInt(data) > 0) {
                var currPage = $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page");
                thisPage.FetchForPage(currPage);
                ShowSuccessMessagePanel('增加成功。');
            }
        }).fail(function (data) {
            ajaxError(data, 1);
        }).always(function () {
        });
    },
    // 删除系统成员操作
    DoDelete: function (userName) {
        showModal(350, 140, '确认删除', '', null, null, 1, '请问，确定要删除当前记录吗？',
                function () {
                    var instance = {};
                    instance['username'] = userName;
                    instance['deletedBy'] = $("#UserId").val();

                    $.ajax({
                        type: "POST",
                        url: parent.basePath + "/admin/membership/delete.html",
                        timeout: 30 * 1000,
                        data: JSON.stringify(instance),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        beforeSend: function () {
                        }
                    }).done(function (data) {
                        if (data > 0) {
                            var currPage = $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page");
                            // 如果删除的是最后一页的最后一条，并且当前页不是首页,重画页面设为前一页
                            if (parseInt($('#tabData01 tbody').find('tr').length) === 1 && currPage > 1) {
                                thisPage.FetchForPage(currPage - 1);
                            } else {
                                thisPage.FetchForPage(currPage);
                            }
                            ShowSuccessMessagePanel('删除成功。');
                        }
                    }).fail(function (data) {
                        ajaxError(data, 1);
                    }).always(function () {
                    });
                });
    }
};

//-----------------------------------------------------------------------------
// 表格排序
//-----------------------------------------------------------------------------
function sortTable(order_by) {
    _order_by = order_by;
    thisPage.FetchForPage(1);
}

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    //设置分页控制
    $("#wrapper__navbar").swPagination({
        link_string: '/?page={page_number}',
        max_page: null,
        paged: function (page) {
            $("abbr.page_indicator_block,#wrapper__navbar").attr("_msh_page", page);
            thisPage.FetchForPage(page);
        }
    });

    // 设置表格排序字段
    $("#tabHead01").wxTableSorter({
        sortList: [[2, 1], [4, 1]], // 第一个表示列的序号，第二个表示排序种类
        headers: {2: {sorter: false}, 4: {sorter: false}},
        columnList: [[1, "MS.UserName"], [2, "E.DepartmentId"]],
        detailList: $("#tabData01"),
        sort: sortTable
    });

    thisPage.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    var h = $(window).height();
    var w = $(window).width();

    //$("#gridWrapper").css({left: 8, width: w - 16});

    over = 1;
}